package github.ziwukun.jsonql.lang;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import github.ziwukun.jsonql.utils.CollectionUtils;
import lombok.NonNull;

/**
 * 搜索项实现类
 * @author Hugo.Yu
 */
public class SelectImpl implements Select{
	private static final long serialVersionUID = -841285088836404274L;
	List<SelectItem> items;
	SelectImpl(@NonNull List<SelectItem> items){
		this.items = new ArrayList<>(items);
		asserts();
	}
	
	@Override
	public List<SelectItem> listItems() {
		return new ArrayList<>(items);
	}

	@Override
	public boolean isItemsEmpty() {
		return items.isEmpty();
	}

	@Override
	public boolean containProp(String propName) {
		if(StrUtil.isBlankIfStr(propName)) {
			return false;
		}
		return CollectionUtils.contains(items, item -> {
			return item.prop().equals(propName);
		});
	}

	@Override
	public Select propSelect(String propName) {
		if(StrUtil.isBlankIfStr(propName)) {
			return null;
		}
		SelectItem item = CollectionUtils.findOne(items, it -> {
			return it.prop().equals(propName);
		});
		return item == null?null:item.childs();
	}

	@Override
	public Select repProp(String propName, String newProp) {
		if(StrUtil.isBlank(propName)) {
			throw new JSONQLBuildException("需要替换的属性名称不能为空");
		}
		if(StrUtil.isBlank(newProp)) {
			throw new JSONQLBuildException("替换的新属性名称不能为空");
		}
		
		int propIndex = -1;
		SelectItem item = null;
		for (int i = 0; i < this.items.size(); i++) {
			if(items.get(i).prop().equals(propName)) {
				propIndex = i;
				item = items.get(i);
			}
		}
		
		if(item == null) {
			throw new JSONQLBuildException("不存在的属性名称{}",propName);
		}
		
		item = item.repProp(newProp);
		List<SelectItem> newItems = ObjectUtil.cloneByStream(items);
		newItems.set(propIndex, item);
		
		return new SelectImpl(newItems);
	}

	@Override
	public Select repProps(Map<String, String> propNames) {
		if(propNames == null || propNames.isEmpty()) {
			return ObjectUtil.clone(this);
		}
		
		// 存放需要被替换的搜索项
		Map<Integer,SelectItem> replaceItems = new HashMap<>();
		for (Map.Entry<String, String> kv: propNames.entrySet()) {
			String oldProp = kv.getKey();
			String newProp = kv.getValue();
			if(StrUtil.isBlank(oldProp)) {
				throw new JSONQLBuildException("需要替换的属性名称不能为空");
			}
			if(StrUtil.isBlank(newProp)) {
				throw new JSONQLBuildException("替换的新属性名称不能为空");
			}
			
			int propIndex = -1;
			SelectItem item = null;
			for (int i = 0; i < this.items.size(); i++) {
				if(items.get(i).prop().equals(oldProp)) {
					propIndex = i;
					item = items.get(i);
				}
			}
			
			if(item == null) {
				throw new JSONQLBuildException("不存在的属性名称{}",oldProp);
			}
			
			replaceItems.put(propIndex, item.repProp(newProp));
		}
		
		List<SelectItem> newItems = ObjectUtil.cloneByStream(items);
		for(Map.Entry<Integer,SelectItem> kv : replaceItems.entrySet()) {
			Integer index = kv.getKey();
			SelectItem newItem = kv.getValue();
			
			newItems.set(index, newItem);
		}
		
		return new SelectImpl(newItems);
	}

	@Override
	public Select add(String prop) {
		SelectItemImpl newItem = new SelectItemImpl(prop, null);
		List<SelectItem> newItems = ObjectUtil.cloneByStream(items);
		newItems.add(newItem);
		
		return new SelectImpl(newItems);
	}

	@Override
	public Select add(SelectItem prop) {
		List<SelectItem> newItems = ObjectUtil.cloneByStream(items);
		newItems.add(prop);
		
		return new SelectImpl(newItems);
	}

	@Override
	public String toJSONQL() {
		return SelectJson.writeJson(this);
	}
	
	/**断言对象完善*/
	private void asserts() {
		Set<String> set = new HashSet<>();
		List<String> list = new LinkedList<>();
		for (SelectItem item : items) {
			set.add(item.prop());
			list.add(item.prop());
		}
		
		if(set.size() != list.size()) {
			list.removeAll(set);
			throw new JSONQLBuildException("不能设置重名搜索属性",
				list.toString());
		}
	}
}
